package com.itxckf.controller;


import com.itxckf.pojo.*;
import com.itxckf.service.OrderService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/getOrder")
    /**
     * 通过 name 搜索
     */
    public Result getOrder(@RequestBody Query name){
        List<Order> list = orderService.getOrder(name.getName(),name.getSts());
        for (Order order : list) {
            List<Products> productsList = orderService.getOrderProducts(order.getOrderID());
            order.setOrderProductsList(productsList);
        }
        log.info("内容，{}",list);
        return Result.success(list);
    };
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order){
        // 获取当前时间
        Date currentDate = new Date();
        order.setOrderDate(currentDate);
        log.info("创建订单,{}",order);
        orderService.addOrder(order);
        // 获取最后一个插入的订单 ID
        int orderId = orderService.getLastInsertedOrderId();
        return Result.success(orderId);
    };
    /**
     * 更新商品订单
     * @param orderList
     * @return
     */
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody Order orderList){
        log.info("更新商品，{}",orderList);
        orderService.updateOrder(orderList);
        return Result.success("修改成功!");
    };

    @PostMapping("/addOrderProducts")
    public Result addOrderProducts(@RequestBody OrderProducts orderProducts){
        log.info("添加商品，{}",orderProducts);
        orderService.addOrderProducts(orderProducts);
        return Result.success("添加成功！");
    };

    @PostMapping("/deleteOrderProducts")
    public Result deleteOrderProducts(@RequestBody DeleteOrderProductsFrom deleteOrderProductsFrom){
        log.info("删除商品，{}",deleteOrderProductsFrom);
        orderService.deleteOrderProducts(deleteOrderProductsFrom);
        return Result.success("删除成功！");
    };

    /**
     * 查询订单信息
     * @param delete
     * @return
     */
    @PostMapping("/queryOrder")
    public Result queryOrder(@RequestBody Delete delete){
        log.info("查询商品，{}",delete.getId());
        Order orderList = orderService.queryOrder(delete.getId());
        List<Products> productsList = orderService.getOrderProducts(delete.getId());
        orderList.setOrderProductsList(productsList);
        return Result.success(orderList);
    };


}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Query {
    private String name;
    private String sts;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Delete {
    private Short id;
}





